import SalesPlanAPI from '../../../common/api/salesplan/salesplanmain.api.js';
import doT from '../../../../vendor/template/doT.js';
import { DictionaryType, requestDict } from "../../../common/dictionary/dictionary-type.js";


(function (window, document, $) {
    // const addSalesplanTemplate = doT.template($('#add-salesplan-template').text());
    const toolbarTemplate = `<div class="toolbar" id="me-date-screening">
<div class="toolbar-inner">
<span style='margin:0 auto;margin-top:10px'>请选择业务日期</span>
<a href="javascript:;" class="picker-button close-picker" style="color:#427ce7;font-size:20px;margin-right:18px;display:inline-block;line-height:100%">{{closeText}}</a>
</div>
</div>`;
    let _vm;
    const looksalesTemplate = doT.template($('#looksalesplan-template').text());
    const lksalesmainTemplate = doT.template($('#looksalesplanmain-template').text());
    $(function () {
        $('body').attr('data-weui-theme', 'light'); //强制光明模式
        _initCommand()
    });

    //返回列表
    $(document).on('click', '#returnlist', function () {
        location.href = `../salesplan/salesplanmain.html?v=${Utils.GetTime()}`;
    });

    const _initCommand = () => {
        SalesPlanAPI.querySalesInfo(getUrlKey("id")).then(res => {
            // 去掉时间，只要日期
            res.bizDate = res.bizDate.split(' ')
            res.deliveryDate = res.deliveryDate.split(' ')
            res.bizDate = res.bizDate[0].replace(/\//g, '-')
            res.deliveryDate = res.deliveryDate[0].replace(/\//g, '-')
            // 数字千分位
            res.entries.forEach(item => {
                item.fassistQty = format(item.fassistQty)
                item.baseQty = format(item.baseQty)
            });
            requestDict(DictionaryType.shipping_TYPE).then(ress => {
                let shippingArr = ress.data.filter(item => {
                    return item.value == res.shippingType
                })
                if (Utils.IsNotNull(shippingArr)) {
                    res.shippingType = shippingArr[0].displayName
                }
                $('#looksalesMain').append(lksalesmainTemplate(res));
            })
            $('#looksalesEntry').append(looksalesTemplate(res.entries));
        })
    }
    /** 获取地址栏的 参数  传入 字段返回值 */
    const getUrlKey = (name) => {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }

    // 千分位
    const format = (num) => {
        var reg = /\d{1,3}(?=(\d{3})+$)/g;
        return (num + '').replace(reg, '$&,');
    }
})(window, document, jQuery)